//
//  StatusPictureView.swift
//  swft_weibo
//
//  Created by mac on 15/9/16.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SDWebImage
class StatusPictureView: UICollectionView {

    var status : Status?{
    
        didSet{
            
            reloadData()
        }
    
    }
    /// 定义流水布局
    let viewLayout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    init(){
    
        super.init(frame: CGRectZero, collectionViewLayout: viewLayout)
        
        dataSource = self
        
        registerClass(StatusPictureViewCell.self, forCellWithReuseIdentifier: "cell")
    }
    
    /**
    计算出其自身的frame值出来
    */
    func computeStatusPictureViewFrame()->CGSize{
    
        let picturesCount = status?.pic_urls?.count
        
        if picturesCount == 0{
        
            return CGSize(width: 0, height: 0)
        }
        
        
        //如果返回的是一张图片，显示图片的原始宽度
        if picturesCount == 1{
            
            //取出缓纯中的图片
            let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(status!.pic_urls!.last!["thumbnail_pic"] as! String )
            
            //设置布局的尺寸
            viewLayout.itemSize=image.size
            //设置collectionview的尺寸
            return image.size
            
        }
        //定义图片的宽度和高度，还有间隔
        let height = CGFloat(90)
        let margin = CGFloat(10)
        //这里设置布局总的itemsize
        viewLayout.itemSize=CGSizeMake(height, height)
        //当配图只有2张的时候
        if picturesCount == 2 {
            //设置collectionview的尺寸
            return CGSizeMake(height*2+margin, height)
        
        }
        //除了上面几种情况，下面是剩下的情况
        if picturesCount == 4 {
            
            return CGSizeMake(height*2+margin, height*2+margin)

        }
        let colums = CGFloat(3)
        let pictureWidth=height*colums+margin*(colums-1)
        let rowNumber = (picturesCount!-1)/3 + 1
        let pictureHeight=CGFloat(rowNumber)*height + (CGFloat(rowNumber)-1)*margin
        
        return CGSizeMake(pictureWidth, pictureHeight)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
/// 自定义cell
class StatusPictureViewCell : UICollectionViewCell {
    
    var imageURL :String?{
    
        didSet{
        
            pictureView?.image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(imageURL)
        }
    
    }
    
   override init(frame: CGRect) {
        super.init(frame: frame)
        
        //添加子控件用于显示图片
        contentView.addSubview(pictureView!)
        
        //布局子控件
        pictureView?.xmg_Fill(contentView)
    }
    
    lazy var pictureView : UIImageView?={
        let pictureView = UIImageView()
        
        return pictureView
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
// MARK: - 实现代理方法
extension StatusPictureView : UICollectionViewDataSource{

    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let collectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! StatusPictureViewCell
        
        collectionViewCell.imageURL=status?.pic_urls![indexPath.item]["thumbnail_pic"] as? String
        return collectionViewCell
        
    }

}



